package edu.clemson.wcbeck.cft;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
//import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class CFTTeamWallpaper extends Activity{

	MediaPlayer mediaPlayer = CFTTeamSongs.mediaPlayer;
	
	Integer[] pics = {
            R.drawable.clemson1, R.drawable.clemson2,
            R.drawable.clemson3, R.drawable.clemson4,
            R.drawable.clemson5, R.drawable.clemson6,
    };
    ImageView imageView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teamwallpaper);
        
        Gallery ga = (Gallery)findViewById(R.id.glryTeamWalls);
        ga.setAdapter(new ImageAdapter(this));
        
        imageView = (ImageView)findViewById(R.id.imgGallery);
        imageView.setImageResource(pics[0]);
        ga.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Toast.makeText(getBaseContext(), 
						"You have selected picture " + (arg2+1), Toast.LENGTH_SHORT).show();
				imageView.setImageResource(pics[arg2]);
				
			}
        	
        });
        
        registerForContextMenu(imageView);
    }
    
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId()==R.id.imgGallery) {
//			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			String[] menuItems = getResources().getStringArray(R.array.strArrContextMenuSongs);
			menu.setHeaderTitle("Options:");

			for (int i = 0; i<menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
//      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      int menuItemIndex = item.getItemId();
      long myItemID = R.id.imgGallery;
//      dataSource.openDataBase();
//      
//      @SuppressWarnings("unchecked")
//      ArrayAdapter<Team> adapter = (ArrayAdapter<Team>) getListAdapter();
//	  Team team = (Team) getListAdapter().getItem(info.position);
	  
      switch(menuItemIndex){
      case 0: //Send
    	  Uri uri = Uri.parse("android.resource://edu.clemson.wcbeck.cft/" + myItemID);
    	  Intent it = new Intent(Intent.ACTION_VIEW);   
    	  it.putExtra("sms_body", "Thanks for using CFT, enjoy!");   
    	  it.setType("vnd.android-dir/mms-sms");
    	  it.putExtra(Intent.EXTRA_STREAM, uri);  
    	  
    	  startActivity(Intent.createChooser(it, "Send Picture"));
    	  
    	  break;
      case 1: //Save
//    	  dataSource.deleteTeam(team);
//    	  adapter.remove(team);
    	  break;
      case 2: //Set
    	  
    	  Uri uri1 = Uri.parse("android.resource://edu.clemson.wcbeck.cft/" + myItemID);
    	  Intent it1 = new Intent(Intent.ACTION_SET_WALLPAPER);      
    	  it1.setType("image/*");
    	  it1.putExtra(Intent.EXTRA_STREAM, uri1);  
    	  
    	  startActivity(it1);
    	  break;
      }
      
//      dataSource.close();
      return true;
    }
	
    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
    	super.onBackPressed();
    }
    
    public class ImageAdapter extends BaseAdapter {

    	private Context ctx;
    	int imageBackground;
    	
    	public ImageAdapter(Context c) {
			ctx = c;
			TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
			imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
			ta.recycle();
		}

		@Override
    	public int getCount() {
    		
    		return pics.length;
    	}

    	@Override
    	public Object getItem(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public long getItemId(int arg0) {
    		
    		return arg0;
    	}

    	@Override
    	public View getView(int arg0, View arg1, ViewGroup arg2) {
    		ImageView iv = new ImageView(ctx);
    		iv.setImageResource(pics[arg0]);
    		iv.setScaleType(ImageView.ScaleType.FIT_XY);
    		iv.setLayoutParams(new Gallery.LayoutParams(150,120));
    		iv.setBackgroundResource(imageBackground);
    		return iv;
    	}

    }
    
}

